package com.oalam.bookstore.domain.books;

import com.oalam.bookstore.dao.BookDao;
import com.oalam.bookstore.dao.DaoFactory;
import com.oalam.bookstore.dao.FileDaoFactory;
import com.oalam.bookstore.dao.MockDaoFactory;
import com.oalam.bookstore.domain.BusinessObjectFactory;
import java.util.List;

/**
 * @author Administrateur
 * @version 1.0
 * @created 28-juil.-2011 16:18:24
 */
public class Catalog {

    private String dbPath;
    private BookDao dao;

    private Catalog() {
        setupDao(false);
    }

    public final void setupDao(boolean doUseMockDao) {
        DaoFactory daoFactory = null;
        if (doUseMockDao) {
            daoFactory = new MockDaoFactory();
        } else {
            daoFactory = new FileDaoFactory();
        }

        dao = daoFactory.createBookDao();
    }

    public List<Book> findBooks(String phrase, boolean doSearchByTitle) {
        if (doSearchByTitle) {
            return dao.findBookByTitle(phrase);
        } else {
            Author a = BusinessObjectFactory.getInstance().createAuthor(phrase);
            return dao.findBookByAuthor(a);
        }
    }

    private static class SingletonHolder {

        private final static Catalog instance = new Catalog();
    }

    public static Catalog getInstance() {
        return SingletonHolder.instance;
    }
}